package com.jiuyi.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuyi.common.R;
import com.jiuyi.dto.AdminDishDto;

import com.jiuyi.entity.Dish;
import com.jiuyi.entity.DishFlavor;

import com.jiuyi.mapper.CategoryMapper;
import com.jiuyi.mapper.DishFlavorMapper;
import com.jiuyi.mapper.DishMapper;
import com.jiuyi.service.DishService;
import com.jiuyi.utils.BeanCopyUtils;
import com.jiuyi.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * DATA: 2022/10/19
 * Author: 晨夕
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    //添加菜品
    @Transactional
    @Override
    public R addDish(AdminDishDto adminDishDto) {
        //添加菜品
        Dish dish = BeanCopyUtils.copyBean(adminDishDto, Dish.class);
        dishMapper.insert(dish);
        List<AdminDishAddVo> flavors = adminDishDto.getFlavors();
        //添加口味
        for(AdminDishAddVo adminDishAddVo : flavors){
            DishFlavor dishFlavor = BeanCopyUtils.copyBean(adminDishAddVo, DishFlavor.class);
            dishFlavor.setDishId(dish.getId());
            dishFlavorMapper.insert(dishFlavor);
        }
        return R.success("添加成功");
    }

    //查询菜品
    @Override
    public R<AdminSelectDishVo> selectDish(int page, int pageSize, String name) {
        //1. 根据姓名查询，并根据修改时间降序排序
        List<Dish> dishList = dishMapper.selectDishList((page - 1) * pageSize,pageSize,name);
        int count = dishMapper.selectDishCount(name);
        List<AdminSelectDishVo> adminSelectDishVos = BeanCopyUtils.copyBeanList(dishList, AdminSelectDishVo.class);
        //对每个分类赋值
        for(AdminSelectDishVo adminSelectDishVo : adminSelectDishVos){
            String categoryName = categoryMapper.selectByCategoryId(adminSelectDishVo.getCategoryId());
            adminSelectDishVo.setCategoryName(categoryName);
        }
        //2. 分页
        PageVo<AdminSelectDishVo> pageVo = new PageVo<>();
        pageVo.setRecords(adminSelectDishVos);
        pageVo.setTotal(count);
        return R.ok(pageVo);
    }
    //根据id查询菜品
    @Override
    public R<AdminUpdateDishVo> selectDishById(Long id) {
        //查询菜品
        Dish dish = dishMapper.selectById(id);
        //根据菜品查询分类
        String categoryName = categoryMapper.selectByCategoryId(dish.getCategoryId());
        AdminUpdateDishVo adminUpdateDishVo = BeanCopyUtils.copyBean(dish, AdminUpdateDishVo.class);
        adminUpdateDishVo.setCategoryId(dish.getCategoryId());
        adminUpdateDishVo.setCategoryName(categoryName);
        //根据菜品查询口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectNameValueById(dish.getId());
        List<AdminDishAddVo> adminDishAddVos = BeanCopyUtils.copyBeanList(dishFlavors, AdminDishAddVo.class);
        adminUpdateDishVo.setFlavors(adminDishAddVos);
        return R.ok(adminUpdateDishVo);
    }
    //修改菜品
    @Override
    public R updateDish(AdminDishDto adminDishDto) {
        //1. 修改菜品表
        Dish dish = BeanCopyUtils.copyBean(adminDishDto, Dish.class);
        dishMapper.updateById(dish);
        //2. 修改口味
        //2.1 先删除全部口味
        dishFlavorMapper.deleteByDishId(dish.getId());
        //2.2 再添加全部口味
        List<AdminDishAddVo> flavors = adminDishDto.getFlavors();
        //添加口味
        for(AdminDishAddVo adminDishAddVo : flavors){
            DishFlavor dishFlavor = BeanCopyUtils.copyBean(adminDishAddVo, DishFlavor.class);
            dishFlavor.setDishId(dish.getId());
            dishFlavorMapper.insert(dishFlavor);
        }
        return R.success("修改成功");
    }
    //删除菜品
    @Override
    public R deleteDish(Long[] ids) {
        for(Long id : ids){
            //1. 删除口味
            List<Long> dishIds = dishFlavorMapper.selectDishIdByid(id);
            for(Long dishId : dishIds){
                dishFlavorMapper.deleteById(dishId);
            }
            //2. 删除菜品
            dishMapper.deleteById(id);
        }
        return R.success("删除成功");
    }
    //停售菜品
    @Override
    public R stopSell(Long[] ids) {
        //将status修改为0
        dishMapper.updateStatusById(ids);
        return R.success("停售成功");
    }
    //起售菜品
    @Override
    public R startSell(Long[] ids) {
        //批量将status修改为1
        dishMapper.updateStatusByIds(ids);
        return R.success("起售成功");
    }
    //查询菜品集合
    @Override
    public R<List<AdminSelectDishListVo>> dishList(Dish dish) {
        //根据dish的id查询集合
        List<Dish> dishList = dishMapper.selectDishListByCategoryId(dish.getCategoryId(),dish.getName());
        List<AdminSelectDishListVo> adminSelectDishListVos = BeanCopyUtils.copyBeanList(dishList, AdminSelectDishListVo.class);
        //根据菜品查询口味
        for(AdminSelectDishListVo adminSelectDishListVo : adminSelectDishListVos){
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectNameValueById(adminSelectDishListVo.getId());
            List<AdminDishAddVo> adminDishAddVos = BeanCopyUtils.copyBeanList(dishFlavors, AdminDishAddVo.class);
            adminSelectDishListVo.setFlavors(adminDishAddVos);
        }
        return R.ok(adminSelectDishListVos);
    }
}
